using System;
using System.ComponentModel.Composition;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SeventhSenseSystems.HighFlyingPm.Business.Entities;
using SeventhSenseSystems.HighFlyingPm.Business.Models;

namespace WebBusiness.Controllers
{   
	[Export("projects",typeof(IController))]
	[PartCreationPolicy(CreationPolicy.NonShared)]
	public class ProjectsController : Controller
	{
		private readonly IProjectRepository projectRepository;

		[ImportingConstructor]
		public ProjectsController(IProjectRepository projectRepository)
		{
			this.projectRepository = projectRepository;
		}

		//
		// GET: /Projects/

		public ViewResult Index()
		{
			return View(projectRepository.AllIncluding(project => project.Trackers));
		}

		//
		// GET: /Projects/Details/5

		public ViewResult Details(System.Guid id)
		{
			return View(projectRepository.Find(id));
		}

		//
		// GET: /Projects/Create

		public ActionResult Create()
		{
			return View();
		} 

		//
		// POST: /Projects/Create

		[HttpPost]
		public ActionResult Create(Project project)
		{
			if (ModelState.IsValid) {
				project.Id = Guid.NewGuid();
				projectRepository.Add(project);
				projectRepository.Save();
				return RedirectToAction("Index");
			} else {
				return View();
			}
		}
		
		//
		// GET: /Projects/Edit/5
 
		public ActionResult Edit(System.Guid id)
		{
			 return View(projectRepository.Find(id));
		}

		//
		// POST: /Projects/Edit/5

		[HttpPost]
		public ActionResult Edit(Project project)
		{
			if (ModelState.IsValid) {
				projectRepository.Edit(project);
				projectRepository.Save();
				return RedirectToAction("Index");
			} else {
				return View();
			}
		}

		//
		// GET: /Projects/Delete/5
 
		public ActionResult Delete(System.Guid id)
		{
			return View(projectRepository.Find(id));
		}

		//
		// POST: /Projects/Delete/5

		[HttpPost, ActionName("Delete")]
		public ActionResult DeleteConfirmed(System.Guid id)
		{
			projectRepository.Delete(projectRepository.Find(id));
			projectRepository.Save();

			return RedirectToAction("Index");
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				projectRepository.Dispose();
			}
			base.Dispose(disposing);
		}
	}
}

